package com.lin.lazybone.ui;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity._User;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2017/10/24.
 */

public class SignActivity extends BaseActivity
{

    private TextView regiter;
    private EditText account;
    private EditText password;
    private EditText confirm_password;
    private EditText tel;
    private EditText email;
    private EditText nikename_sign;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toorbal);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        init();
        regiter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account_num = account.getText().toString();
                String pwd = password.getText().toString();
                String confirm_pwd = confirm_password.getText().toString();
                String phone = tel.getText().toString();
                String mail = email.getText().toString();
                String nikeName = nikename_sign.getText().toString();
                if(account_num.equals("")||pwd.equals("")||confirm_pwd.equals("")||phone.equals("")||mail.equals("")){
                    Toast.makeText(SignActivity.this, "数据不能为空", Toast.LENGTH_SHORT).show();
                }else{
                    if(password.getText().toString().equals(confirm_password.getText().toString())){
                        _User new_user = new _User();
                        new_user.setUsername(account_num);
                        new_user.setPassword(pwd);
                        new_user.setMobilePhoneNumber(phone);
                        new_user.setEmail(mail);
                        new_user.setUser_nickname(nikeName);
                        new_user.setUser_sex("男");
                        new_user.setUser_signature("这个人很懒，什么都没有留下！");
                        new_user.signUp(new SaveListener<_User>() {
                            @Override
                            public void done(_User user, BmobException e){
                                if(e == null){
                                    Toast.makeText(SignActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                }else{
                                    if(e.getErrorCode() == 209)
                                    {
                                        Toast.makeText(SignActivity.this, "该手机号已存在，请重新输入", Toast.LENGTH_SHORT).show();
                                    }else
                                    {
                                        Toast.makeText(SignActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });

                    }else{
                        Toast.makeText(SignActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
    }
    public void init(){
        regiter = (TextView) findViewById(R.id.register);
        account = (EditText) findViewById(R.id.account);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);
        confirm_password = (EditText) findViewById(R.id.confirm_password);
        tel = (EditText)findViewById(R.id.telphone);
        nikename_sign = (EditText) findViewById(R.id.nikename_sign);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}

